//搜索
$(document).on('click', '.main-tit-seach span', function () {
    var that = $(this);
    var keyword = that.prev().find('input').val();
    if (keyword.length < 1) {
        layer.msg('请输入关键词');
        return false;
    }
    var params = getParams();
    params.keyword = keyword;
    var param_array = [];
    Object.keys(params).forEach(function (key) {
        param_array.push(key + '=' + params[key]);
    });
    var param_str = param_array.length > 0 ? '?' + param_array.join('&') : '';
    window.location.href = "/admin/adminer/lists.html" + param_str;
});


//修改按钮
$(document).on('click', '.show-body .main .tit-li6-p2', function () {
    var that = $(this);
    var name = that.parent().siblings('.tit-li1').html();
    var id = that.parent().attr('data-id');
    $('.details-name input').val(name);
    $('#rule_id').val(id);
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "/admin/authgroup/getRule",
        data: {id: id},
        success: function (res) {
            if (res.code == 1) {
                $('.power-tree').html(res.data);
                $('.power-pack-show').hide();
                $('.power-pack-details').show();
            } else {
                layer.msg(res.msg);
            }
        }
    });
});


//返回
$(document).on('click', '.account-add-return', function () {
    $('.account-set').show();
    $('.account-add').hide();
});

//监听提交
layui.use(['form', 'layer', 'jquery', 'laydate'], function () {
    var form = layui.form
        , layer = layui.layer
        , laydate = layui.laydate
        , $ = layui.jquery;


    //套餐选择
    form.on('select(role_select)', function (data) {
        if (data.value == '') {
            console.log('添加套餐');
            window.location.href = "/admin/authgroup/lists.html";
            return false;
        }
        var params = getParams();
        params.roleid = data.value;
        var param_array = [];
        Object.keys(params).forEach(function (key) {
            param_array.push(key + '=' + params[key]);
        });
        var param_str = param_array.length > 0 ? '?' + param_array.join('&') : '';
        window.location.href = "/admin/adminer/lists.html" + param_str;
    });

    //分组选择
    form.on('select(group_select)', function (data) {
        var params = getParams();
        params.groupid = data.value;
        var param_array = [];
        Object.keys(params).forEach(function (key) {
            param_array.push(key + '=' + params[key]);
        });
        var param_str = param_array.length > 0 ? '?' + param_array.join('&') : '';
        window.location.href = "/admin/adminer/lists.html" + param_str;
    });

    //下拉菜单
    //监听提交
    form.on('submit(formDemo)', function (data) {
        layer.msg(JSON.stringify(data.field));
        return false;
    });

    //开启关闭按钮
    //监听指定开关
    form.on('switch(switchTest)', function (data) {
        if (data.elem.checked == true) {
            layer.msg(
                '已启用',
                {time: 1000,}
            );
            $(this).parents('ul').find('.tit-li1')
        } else {
            layer.msg(
                '已停用',
                {time: 1000,}
            );
        }
    });


    //添加账号
    $('.main-tit-newadd').on('click', function () {
        $('.account-set').hide();
        $('.account-add').show();
        $('.account-add-body .bread-crumbs-nav .span2').html('添加账号');
        //表单初始赋值
        form.val('form-action', {
            "id": 0,
            "username": '',
            "password": '',
            "confirm_password": '',
            "realname": '',
            "role_id": 0,
            "group_id": 0,
        });
        var now = new Date();
        //开始时间
        laydate.render({
            elem: '#start-time',
            type: 'datetime',
            value: now
        });
        //结束时间
        laydate.render({
            elem: '#end-time',
            type: 'datetime',
            value: new Date(now.setMonth(now.getMonth() + 1))
        });
    });

    //点击账号列表修改
    $(document).on('click', '.account-set .tit-li9 .span2', function () {
        $('.account-add-body .bread-crumbs-nav .span2').html('修改账号');
        var id = $(this).parent('.tit-li9').attr('data-id');
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: "/admin/adminer/getInfo",
            data: {id: id},
            success: function (res) {
                if (res.code == 1) {
                    //表单初始赋值
                    form.val('form-action', {
                        "id": res.data.id,
                        "username": res.data.username,
                        "password": '',
                        "confirm_password": '',
                        "realname": res.data.realname,
                        "role_id": res.data.role_id,
                        "group_id": res.data.group_id,
                        "start_time": res.data.start_time,
                        "end_time": res.data.end_time,
                    });
                    $('.account-set').hide();
                    $('.account-add').show();
                } else {
                    layer.msg(res.msg);
                }
            }
        });
    });

    //提交用户账号
    form.on('submit(form_account)', function (res) {
        console.log(res.field);
        //判断分页列表长度是否大于分页20，如果不足20，则新增时则追加到管理员列表
        var ul_length = $('.main-table-body ul').length;
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: "/admin/adminer/publish",
            data: res.field,
            success: function (res2) {
                if (res2.code == 1) {
                    if (parseInt(res.field.id) == 0) {
                        if (ul_length < 20) {
                            var html = '<ul>';
                            html += '<li class="tit-li1">' + res2.data.id + '</li>';
                            html += '<li class="tit-li2">' + res2.data.username + '</li>';
                            html += '<li class="tit-li3">' + res2.data.auth_name + '</li>';
                            html += '<li class="tit-li4">' + res2.data.group_name + '</li>';
                            html += '<li class="tit-li5">' + res2.data.realname + '</li>';
                            html += '<li class="tit-li6">' + res2.data.ip + '</li>';
                            html += '<li class="tit-li7">' + res2.data.lasttime + '</li>';
                            html += '<li class="tit-li8">' + res2.data.status_info + '</li>';
                            html += '<li class="tit-li9" data-id="' + res2.data.id + '">';
                            html += '   <span class="span1">登录</span>';
                            html += '   <span class="span2">修改</span>';
                            html += '   <span class="span3">删除</span>';
                            html += '</li>';
                            html += '</ul>';
                            $(".main-table-body").append(html);
                        }
                    }
                    $('.account-set').show();
                    $('.account-add').hide();
                } else {
                    layer.msg(res2.msg);
                }
            }
        });
    })
});


//开始时间
layui.use('laydate', function () {
    var laydate = layui.laydate;
    var myDate = new Date();
    //执行一个laydate实例
    laydate.render({
        elem: '#start-time',
        type: 'datetime',
        value: myDate
    });
});
//结束时间
layui.use('laydate', function () {
    var laydate = layui.laydate;
    var myDate = new Date();
    var Yea = myDate.getFullYear();
    var Mon = myDate.getMonth() + 2;
    var Day = myDate.getDate();
    var Hou = myDate.getHours();
    var Min = myDate.getMinutes();
    var Sec = myDate.getSeconds();
    //执行一个laydate实例
    laydate.render({
        elem: '#end-time',
        type: 'datetime',
        value: myDate
    });
});


//分组管理
$(document).on('click', '.main-tit-formadd', function () {
    $('.account-set').hide();
    $('.account-group').show();
    $('.account-add-body .bread-crumbs-nav .span2').html('分组管理');
});


//返回
$(document).on('click', '.account-group-return', function () {
    $('.account-set').show();
    $('.account-group').hide();
});

//添加分组
$(document).on('click', '.account-group-add', function () {
    layer.open({
        title: ['添加组', 'font-size:14px;text-align:center;color:#424f65;'],
        type: 1,
        skin: 'layui-layer-new', //加上边框
        area: ['540px', '450px'], //宽高
        move: false,
        closeBtn: 2,
        shade: 0.6,
        btn: ['保存', '取消'],
        shadeClose: true,
        content: '<div class="account-group-pop">' +
        '<form class="layui-form" action="">' +
        '<div class="main-list main-number">' +
        '<p class="left-tit">序号：</p>' +
        '<div class="main-body"><input type="text" name="listorder" id="group_listorder" value="0" lay-verify="required|number" placeholder="请输入序号" class="layui-input"></div>' +
        '</div>' +
        '<div class="main-list main-name">' +
        '<p class="left-tit">组名：</p>' +
        '<div class="main-body"><input type="text" name="name" id="group_name" lay-verify="required" placeholder="请输入组名" class="layui-input"></div>' +
        '</div>' +
        '<div class="main-list main-describe">' +
        '<p class="left-tit">描述：</p>' +
        '<div class="main-body"><input type="text" name="remark" id="group_remark" lay-verify="required" placeholder="请输入描述" class="layui-input"></div>' +
        '</div>' +
        '<div class="main-list main-state">' +
        '<p class="left-tit">状态：</p>' +
        '<div class="main-body"><input type="checkbox" name="status" id="group_status" lay-skin="switch" lay-text="启用|停用" checked lay-filter="switchTest"></div>' +
        '</div>' +
        '</form>' +
        '</div>'
        , yes: function (index, layero) {
            //按钮【按钮一】的回调
            var form_value = {};
            form_value.name = $('#group_name').val();
            form_value.remark = $('#group_remark').val();
            form_value.listorder = $('#group_listorder').val();
            form_value.status = $('#group_status').val();
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: "/admin/adminer/groupPublish.html",
                data: form_value,
                success: function (res) {
                    console.log(res);
                    if (res.code == 1) {
                        var html = '<ul>';
                        html += '<li class="tit-li1"><input type="text" name="listorder" value="' + res.data.listorder + '"></li>';
                        html += '<li class="tit-li2"><span>' + res.data.name + '</span><input type="text" style="display:none;"></li>';
                        html += '<li class="tit-li3"><span>' + res.data.remark + '</span><input type="text" style="display:none;"></li>';
                        html += '<li class="tit-li4">' + res.data.create_time + '</li>';
                        html += '<li class="tit-li5">' + res.data.update_time + '</li>';
                        html += '<li class="tit-li6">';
                        html += '   <div>';
                        html += '       <form class="layui-form" action="">';
                        html += '           <div class="layui-form-item">';
                        html += '               <div class="layui-input-block">';
                        html += '                   <input type="checkbox" name="yyy" lay-skin="switch" lay-text="启用|停用" checked="" value="1" lay-filter="switchTest">';
                        html += '                   <div class="layui-unselect layui-form-switch layui-form-onswitch" lay-skin="_switch"><em>启用</em><i></i></div>';
                        html += '               </div>';
                        html += '           </div>';
                        html += '       </form>';
                        html += '   </div>';
                        html += '</li>';
                        html += '<li class="tit-li7" data-id="17"><span class="span1">修改</span><span class="span3" style="display:none;">确定</span><span class="span2">删除</span></li>';
                        html += '</ul>';
                        $('.body-list').find('ul').eq(0).before(html);
                        layui.use('form', function () {
                            var form = layui.form;
                            form.render();
                        });
                        layer.msg('新增成功');
                        layer.close(index);
                    } else {
                        layer.msg(res.msg);
                    }
                }
            });
        }
        , btn2: function (index, layero) {
            //按钮【按钮二】的回调
            layer.close(index);
        }
    });

    //初始化
    layui.use('form', function () {
        var form = layui.form;
        form.render();
    });

});


//修改分组
$(document).on('click', '.account-group-main .body-list .tit-li7 .span1', function () {
    $(this).hide();
    $(this).siblings('.span3').show();
    var Span2 = $(this).parent().parent().find('.tit-li2').children('span');
    var Span3 = $(this).parent().parent().find('.tit-li3').children('span');
    var Input2 = $(this).parent().parent().find('.tit-li2').children('input');
    var Input3 = $(this).parent().parent().find('.tit-li3').children('input');
    Span2.hide();
    Span3.hide();
    Input2.show().val(Span2.html());
    Input3.show().val(Span3.html());
});

//确定修改
$(document).on('click', '.account-group-main .body-list .tit-li7 .span3', function () {
    var that = $(this);
    var Span2 = that.parent().parent().find('.tit-li2').children('span');
    var Span3 = that.parent().parent().find('.tit-li3').children('span');
    var Input2 = that.parent().parent().find('.tit-li2').children('input');
    var Input3 = that.parent().parent().find('.tit-li3').children('input');
    var id = that.parent('.tit-li7').attr('data-id');
    var name = Input2.val();
    var remark = Input3.val();
    var listorder = that.parent().parent().find('.tit-li1').children('input').val();
    if ($.trim(name) == "") {
        layer.msg('组名称不能为空');
        return false;
    }
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: "/admin/adminer/groupEdit.html",
        data: {id: id, name: name, remark: remark, listorder: listorder},
        success: function (res) {
            if (res.code == 1) {
                Span2.show().html(name);
                Span3.show().html(remark);
                Input2.hide();
                Input3.hide();
                $(this).hide();
                $(this).siblings('.span1').show();
                layer.msg('修改成功');
            } else {
                layer.msg(res.msg);
            }
        }
    });
});














